package vg.modules.conveyor.components;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.LinkedHashMap;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.filechooser.FileFilter;

import vg.services.data_base_manager.data.header.GraphModelHeader;
import vg.services.main_manager.MainManager;

public class ConveyorSettingsDialog {
	// Defines
	private final static String DEF_LAST_CONVEYOR_JAR_DIRECTORY = "DEF_LAST_CONVEYOR_JAR_DIRECTORY";
	
	// Swing components
	private static JComponent[] dialogComponents = null;
	private static JLabel modelLabel;
	private static JComboBox modelComboBox;
	private static JLabel jarPathLabel;
	private static JTextField jarPathTextField;
	private static JButton jarPathButton;
	
	// Mutex
	private static final Object generalMutex = new Object();
	
	public static LinkedHashMap<GraphModelHeader, String> show(LinkedHashMap<GraphModelHeader, String> models, int currentModelId) {
		synchronized (generalMutex) {
			LinkedHashMap<GraphModelHeader, String> old = new LinkedHashMap<GraphModelHeader, String>(models);
			createComponents4Dialog(models, currentModelId);
			if (JOptionPane.showConfirmDialog(null, dialogComponents, "Conveyor settings", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE) == 0) {
				return models;
			}
			return old;
		}
	}
	
//=============================================================================
//-----------------PRIVATE METHODS---------------------------------------------
	private static void createComponents4Dialog(final LinkedHashMap<GraphModelHeader, String> models, final int currentModelId) {
		synchronized (generalMutex) {
			if (!SwingUtilities.isEventDispatchThread()) {
				try {
					SwingUtilities.invokeAndWait(new Runnable() {
						@Override
						public void run() {
							synchronized (generalMutex) {
								createComponents4Dialog(models, currentModelId);									
							}
						}
					});
				} catch (Exception ex) {
					MainManager.log.printException(ex);
				}
			} else {
				modelLabel = new JLabel("model name:");
				String[] objs = new String[models.size()];
				int i = 0, selectedIdx = 0;
				GraphModelHeader currentModelHeader = null;
				for (GraphModelHeader header : models.keySet()) {
					objs[i] = header.name;
					if (header.id == currentModelId) {
						selectedIdx = i;
						currentModelHeader = header;
					}
					i++;
				}
				modelComboBox = new JComboBox(objs);
				modelComboBox.setSelectedIndex(selectedIdx);
				modelComboBox.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						int selectedIdx = modelComboBox.getSelectedIndex();
						int i = 0;
						GraphModelHeader currentModelHeader = null;
						for (GraphModelHeader header : models.keySet()) {
							if (i == selectedIdx) {
								currentModelHeader = header;
								break;
							}
						}
						jarPathTextField.setText(models.get(currentModelHeader));
					}
				});
				
				jarPathLabel = new JLabel("jar path:");
				jarPathTextField = new JTextField(models.get(currentModelHeader));
				jarPathTextField.addCaretListener(new CaretListener() {
					@Override
					public void caretUpdate(CaretEvent e) {
						int selectedIdx = modelComboBox.getSelectedIndex();
						int i = 0;
						GraphModelHeader currentModelHeader = null;
						for (GraphModelHeader header : models.keySet()) {
							if (i == selectedIdx) {
								currentModelHeader = header;
								break;
							}
						}
						models.put(currentModelHeader, jarPathTextField.getText());
					}
				});
				
				jarPathButton = new JButton("Browse");
				jarPathButton.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						final JFileChooser fileChooser = new JFileChooser(".");
						String lastDir = MainManager.config.getProperty(DEF_LAST_CONVEYOR_JAR_DIRECTORY);
						if(lastDir != null) 
							fileChooser.setCurrentDirectory(new File(lastDir));
													
				       	fileChooser.addChoosableFileFilter(new FileFilter() {
							public String getDescription() {				
								return "Jar file (*.jar)";
							}
							public boolean accept(File f) {
								if (f != null) {
									if (f.isDirectory())
										return true;
									return f.getName().toLowerCase().endsWith("jar");
								}
								return false;
							}
						});
				       	
						if (fileChooser.showOpenDialog(MainManager.pluginParameter.userInterface.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
							MainManager.config.setProperty(DEF_LAST_CONVEYOR_JAR_DIRECTORY, fileChooser.getCurrentDirectory().getAbsolutePath());
							int selectedIdx = modelComboBox.getSelectedIndex();
							int i = 0;
							GraphModelHeader currentModelHeader = null;
							for (GraphModelHeader header : models.keySet()) {
								if (i == selectedIdx) {
									currentModelHeader = header;
									break;
								}
							}
							jarPathTextField.setText(fileChooser.getSelectedFile().getPath());
							models.put(currentModelHeader, fileChooser.getSelectedFile().getPath());
						}	
					}
				});
				
				dialogComponents = new JComponent[5];
				dialogComponents[0] = modelLabel;
				dialogComponents[1] = modelComboBox;
				dialogComponents[2] = jarPathLabel;
				dialogComponents[3] = jarPathTextField;
				dialogComponents[4] = jarPathButton;
			}
		}
	}
}
